package com.study.system.edusearchservice.controller;

import com.edu.common.entity.PageEntity;
import com.study.system.edusearchservice.service.CourseIndexService;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;


/**
 * 课程索引控制器
 */
@RestController
@RequestMapping("/search")
public class CourseIndexController {

    @Resource
    private CourseIndexService courseIndexService;

    @GetMapping("/init")
    public ResponseEntity<String> initIndex(){
        try {
            courseIndexService.initCourseIndex();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return ResponseEntity.ok("课程索引初始化完成");
    }

    @RequestMapping("/courses")
    public ResponseEntity<PageEntity> searchCourse(@RequestBody Map<String,String> map){
        try {
            PageEntity body = courseIndexService.searchCourse(map);
            return ResponseEntity.ok(body);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
